/* 1624. 两个相同字符之间的最长子字符串  */
/**
 * @param {string} s
 * @return {number}
 */
var maxLengthBetweenEqualCharacters = function (s) {
	let res = -1
	const n = s.length
	for (let i = 0; i < n; i++) {
		for (let j = n - 1; i >= i; j--) {
			if (s[i] === s[j]) {
				res = Math.max(res, j - i - 1)
				break
			}
		}
	}
	return res
}
// 直接遍历
var maxLengthBetweenEqualCharacters = function (s) {
	const firstIndex = new Array(26).fill(-1)
	let maxLength = -1
	for (let i = 0; i < s.length; i++) {
		if (firstIndex[s[i].charCodeAt() - 'a'.charCodeAt()] < 0) {
			firstIndex[s[i].charCodeAt() - 'a'.charCodeAt()] = i
		} else {
			maxLength = Math.max(
				maxLength,
				i - firstIndex[s[i].charCodeAt() - 'a'.charCodeAt()] - 1
			)
		}
	}
}
maxLengthBetweenEqualCharacters('cbzxy')
